﻿using System.Data;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using Dapper;
using MySql.Data.MySqlClient;
using Microsoft.Extensions.Configuration;

using Read.Models;

namespace Read.Services
{
    public class BaseService
    {
        //读取配置文件
        //GlobalService是自定义的类
        protected static string connString = GlobalService.Configuration.GetConnectionString("MySqlConnection");
        //protected static string connString = "server=192.168.16.112;database=reads;uid=root;pwd=root;charset='utf8'";
        //protected IDbConnection connection;

        // /// <summary>
        // /// 构造函数
        // /// </summary>
        // public BaseService()
        // {
        //     using (connection = new MySqlConnection(connString))
        //     {
        //         //这里这样写using其实是有问题的，应该是写到connection使用的地方
        //     }
        // }

        /// <summary>
        /// 实例化连接
        /// </summary>
        public IDbConnection GetConnection()
        {
            return new MySqlConnection(connString);
        }
        
        // /// <summary>
        // /// 关闭链接
        // /// </summary>
        // public void CloseConnection()
        // {
        //     if (connection == null) return;
        //     connection.Close();
        //     connection.Dispose();
        //     connection = null;
        // }
    }
}
